<?php
/**
 * YZW与DZ操作的接口
 */
class DzApi{
	public $userInfo = array();//用户的信息
    public $arrSendData = array();//需要发送的信

	private function __construct($userInfo){
		$this->userInfo = empty($userInfo) ? yii::app()->user->getInfo() : $userInfo;
	}
    static public function getInstance($userInfo = '') {
        static $handier = NULL;
        if (empty($handier)) {
            $handier = new DzApi($userInfo);
        }
        return $handier;
    }
    /**
     * 发贴接口
     * arrData 里必须含有键值,fid,subject,message
     * @return [type]          
     */
    public function post($arrData){
        if(empty($arrData['fid']) || empty($arrData['subject']) || empty($arrData['message'])){
            return false;
        }        
        $arrData['action'] = 'post';
        $arrData['uid'] = $this->userInfo['uid'];
        $this->arrSendData = $arrData;
        return $this->sendCurl();
    }
    /**
     * 验证用户是否有在论坛激活
     * @return [type] [description]
     */
    public function canSunsingle(){
        $this->arrSendData = array(
            'username' => $this->userInfo['username'],
            'uid' => $this->userInfo['uid'],
            'action' => 'activation',
        );
        return $this->sendCurl();            
    }

    public function sendCurl(){
        $arrData = json_encode($this->arrSendData, true);
        $arrData = Encrypt::authcode($arrData, '', Yii::app()->params['bbsInfo']['key']); //加密
        $arrData = array('sign' => $arrData);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, Yii::app()->params['bbsInfo']['url'] . "/yzw_api.php");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $arrData);
        curl_setopt($ch, CURLOPT_NOBODY, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output = json_decode($output, true);  
    }	
}